
package cn.xabad.commons.plist;
import android.util.Log;

import java.lang.String;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;


public class PDate extends PListObject implements
		IPListSimpleObject<java.util.Date> {

	
	private static final long serialVersionUID = 3846688440069431376L;

	private static final String TAG = "Date";

	
	protected java.util.Date date;

	
	private SimpleDateFormat iso8601Format;

	public PDate() {
		setType(PListObjectType.DATE);
		iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
	}

	
	@Override
	public java.util.Date getValue() {
		return date;
	}

	
	@Override
	public void setValue(java.util.Date val) {
		this.date = val;
	}

	
	@Override
	public void setValue(String val) {
		
		if (null == val || val.length() < 1) {
			this.date = null;
			return;
		}
		Scanner scanner = new Scanner(val).useDelimiter("-");
		if (scanner.hasNextInt()) {
			try {
				this.date = iso8601Format.parse(val);
			} catch (ParseException e) {
				Log.e(TAG, new StringBuilder("#setValue - error parsing val=")
                        .append(val).toString(), e);
			}
		} else {
			this.date = new java.util.Date(java.util.Date.parse(val.trim()));
		}
	}
}
